package cn.tsoft.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.tsoft.formbeam.StudentForm;
import cn.tsoft.utils.StreamUtils;


public class StudentManageAction extends Action {
	
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		StudentForm formbean = (StudentForm)form;
		if("GET".equals(request.getMethod())){
			byte[] data = request.getParameter("title").getBytes("ISO-8859-1");
			String title = new String(data, "UTF-8");
			System.out.println("title:"+ title);
			System.out.println("age:"+ formbean.getAge());
		}else{
			System.out.println("name:"+ formbean.getName());
			System.out.println("age:"+ formbean.getAge());
		}
		
		if(formbean.getStudent()!=null && formbean.getStudent().getFileSize()>0){
			String realpath = request.getSession().getServletContext().getRealPath("/video");
			System.out.println(realpath);
			File dir = new File(realpath);
			if(!dir.exists()) dir.mkdirs();
			File videoFile = new File(dir, formbean.getStudent().getFileName());
			FileOutputStream outStream = new FileOutputStream(videoFile);
			outStream.write(formbean.getStudent().getFileData());
			outStream.close();
		}
		return mapping.findForward("result");
	}
	
	public ActionForward getXML(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		InputStream inStream = request.getInputStream();
		byte[] data = StreamUtils.readInputStream(inStream);
		String xml = new String(data, "UTF-8");
		System.out.println(xml);
		return mapping.findForward("result");
	}
}
